package com.suoluo.zenapp.controllers.api.dest;

import java.util.List;

import javax.annotation.Resource;

import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Post;

import org.springframework.util.StringUtils;

import com.suoluo.zenapp.common.APPConstants;
import com.suoluo.zenapp.controllers.JSONInterceptor.JSON;
import com.suoluo.zenapp.pojo.dest.DestExtPOJO;
import com.suoluo.zenapp.pojo.dest.DestImgResourcePOJO;
import com.suoluo.zenapp.pojo.dest.DestResourceExtPOJO;
import com.suoluo.zenapp.pojo.dest.DestTextResourcePOJO;
import com.suoluo.zenapp.pojo.dest.DestTrafficPOJO;
import com.suoluo.zenapp.pojo.dest.DestVersionPOJO;
import com.suoluo.zenapp.service.dest.DestService;
import com.suoluo.zenapp.vo.DestResourceVO;
import com.suoluo.zenapp.vo.DestRespVO;
import com.suoluo.zenapp.vo.DestVO;
import com.suoluo.zenapp.vo.ImageRespVO;
import com.suoluo.zenapp.vo.TextExtRespVO;
import com.suoluo.zenapp.vo.TripDayItemVO;
import com.suoluo.zenapp.vo.TripDayVO;
import com.suoluo.zenapp.vo.TripDestVO;
import com.suoluo.zenapp.vo.TripInfoVO;
import com.suoluo.zenapp.vo.TripVO;

@Path("/")
public class AppDestController {

	@Resource(name = "destService")
	private DestService destService;

	/**
	 * 获取目的地资源信息
	 * 
	 * @param inv
	 * @return
	 */
	@JSON
	@Post("resources")
	public DestRespVO syncdata(Invocation inv) {
		
		int destId = Integer.parseInt(inv.getParameter("destId"));
		
		DestRespVO respVO = new DestRespVO();
		respVO.setDestId(destId);
		//1. 获取库里保存的版本信息
		DestVersionPOJO pojo = destService.findVersionByDestId(destId);
		//2.如果版本号相等，说明未升级
		int versionNum = Integer.parseInt(inv.getParameter("dataVersion"));
		if(versionNum == pojo.getVersion())
		{
			respVO.setDataVersion(pojo.getVersion());
			respVO.setResultCode(APPConstants.NO_UPGRADE);
			respVO.setResultMsg(APPConstants.NO_UPGRADE_MSG);
			return respVO;
		}
		respVO.setDataVersion(pojo.getVersion());
		
		List<DestVO> list = destService.findDestAllResource(destId);
		//2.如果结果集为null
		if (list.isEmpty()) {
			respVO.setResultCode(APPConstants.NO_DEST);
			respVO.setResultMsg(APPConstants.NO_DEST_MSG);
		} else {
			respVO.setResultCode(APPConstants.SUCCESS);
			respVO.setResultMsg(APPConstants.SUCCESS_MSG);
			respVO.setResultData(list);
		}
		//3.查询app引用的目的地Id
		DestResourceVO dest = destService.findDestById(destId);
		int relId = dest.getRelId();
		//4.查询目的地所在的行程信息
		List<TripVO> trips = destService.findListTrips(relId);
		respVO.setTrips(trips);
		return respVO;
	}

	/**
	 * 查询目的地资源的所有图片
	 * 
	 * @param inv
	 * @return
	 */
	@JSON
	@Post("resources/images")
	public ImageRespVO destImg(Invocation inv) {
		int destId = Integer.parseInt(inv.getParameter("destId"));
		
		ImageRespVO respVO = new ImageRespVO();
		respVO.setDestId(destId);
		List<DestImgResourcePOJO> listImg = destService.findDestImg(destId);
		if (listImg.isEmpty()) {
			respVO.setResultCode(APPConstants.NO_DEST);
			respVO.setResultMsg(APPConstants.NO_DEST_MSG);
		} else {
			respVO.setResultCode(APPConstants.SUCCESS);
			respVO.setResultMsg(APPConstants.SUCCESS_MSG);
			respVO.setResultData(listImg);
		}
		return respVO;
	}

	/**
	 * 查询目的地的文本信息和扩展信息
	 * @param destId
	 * @return
	 */
	@JSON
	@Post("resources/dest")
	public TextExtRespVO findDestTextAndExt(@Param("destId")int destId)
	{
		TextExtRespVO textExtVO = new TextExtRespVO();
		if(destId == 0)
		{
			textExtVO.setResultCode(APPConstants.NO_ITEM_ID);
			textExtVO.setResultMsg(APPConstants.NO_ITEM_ID_MSG);
			return textExtVO;
		}
		// 获取文本信息
		DestTextResourcePOJO resourceText = destService
				.findDestTextResourcePOJOById(destId);
		//获取目的地扩展信息
		DestExtPOJO destExt = destService.findDestExtById(destId);
		// 返回数据
		textExtVO.setResourceText(resourceText);
		textExtVO.setResourceId(destId);
		textExtVO.setResultCode(APPConstants.SUCCESS);
		textExtVO.setResultMsg(APPConstants.SUCCESS_MSG);
		textExtVO.setDestExt(destExt);
		return textExtVO;
		
	}
	/**
	 * 查询目的地的文本信息和扩展信息
	 * @param destId
	 * @return
	 */
	@JSON
	@Post("resources/category")
	public TextExtRespVO findDestResourceText(@Param("resourceId")int resourceId)
	{
		TextExtRespVO textExtVO = new TextExtRespVO();
		if(resourceId == 0)
		{
			textExtVO.setResultCode(APPConstants.NO_ITEM_ID);
			textExtVO.setResultMsg(APPConstants.NO_ITEM_ID_MSG);
			return textExtVO;
		}
		// 获取文本信息
		DestTextResourcePOJO resourceText = destService
				.findDestTextResourcePOJOById(resourceId);
		// 返回数据
		textExtVO.setResourceText(resourceText);
		textExtVO.setResourceId(resourceId);
		textExtVO.setResultCode(APPConstants.SUCCESS);
		textExtVO.setResultMsg(APPConstants.SUCCESS_MSG);
		return textExtVO;
		
	}
	/**
	 * 查询资源项文本信息和扩展信息
	 * 
	 * @param inv
	 * @return
	 */
	@JSON
	@Post("resources/item")
	public TextExtRespVO textExt(Invocation inv) {
		TextExtRespVO textExtVO = new TextExtRespVO();
		String resourceIdStr = inv.getParameter("resourceId");
		
		if(!StringUtils.hasLength(resourceIdStr))
		{
			textExtVO.setResultCode(APPConstants.NO_ITEM_ID);
			textExtVO.setResultMsg(APPConstants.NO_ITEM_ID_MSG);
			return textExtVO;
		}
		int resourceId = Integer.parseInt(resourceIdStr);
		// 获取文本信息
		DestTextResourcePOJO resourceText = destService
				.findDestTextResourcePOJOById(resourceId);
		// 获取扩展信息
		DestResourceExtPOJO resourceExt = destService
				.findDestResourceExtPOJOById(resourceId);
		// 返回数据
		textExtVO.setResourceText(resourceText);
		textExtVO.setResourceId(resourceId);
		textExtVO.setResultCode(APPConstants.SUCCESS);
		textExtVO.setResultMsg(APPConstants.SUCCESS_MSG);
		textExtVO.setResourceExt(resourceExt);
		return textExtVO;
	}
	/**
	 * 查询目的地指定类型的交通信息
	 * @param destId
	 * @param typeId
	 * @return
	 */
	@JSON
	@Post("resources/traffics")
	public DestRespVO findDestTraffic(@Param("destId")int destId,@Param("trafficType")int typeId)
	{
		DestRespVO destVO = new DestRespVO();
		destVO.setDestId(destId);
		if(destId == 0 || typeId == 0)
		{
			destVO.setResultCode(APPConstants.NO_ITEM_ID);
			destVO.setResultMsg(APPConstants.NO_ITEM_ID_MSG);
			return destVO;
		}
		// 获取交通信息
		List<DestTrafficPOJO> traffics = destService.findListDestTrafficByTypeId(destId,typeId);
		// 返回数据
		destVO.setResultCode(APPConstants.SUCCESS);
		destVO.setResultMsg(APPConstants.SUCCESS_MSG);
		destVO.setTraffics(traffics);
		return destVO;
	}
	@JSON
	@Post("trip")
	public TripInfoVO findTripDetail(@Param("tripId")int tripId)
	{
		TripInfoVO tripInfoVO = new TripInfoVO();
		//查询行程的目的地
		List<TripDestVO> list = destService.findListTripDest(tripId);
		tripInfoVO.setTripDests(list);
		//查询每日行程和行程项
		List<TripDayVO> tripDays = destService.findListTripDays(tripId);
		tripInfoVO.setTripDays(tripDays);
		
		//查询行程下所有的行程项信息
		List<TripDayItemVO> tripItems = destService.findListTripItem(tripId);
		tripInfoVO.setTripItems(tripItems);
//		Map<Integer,TripCategoryVO> tripCategoryMap = new HashMap<Integer, TripCategoryVO>();
//		
//		for (TripDayVO dayVO : tripDayList){
//			for (TripDayItemVO itemVO : dayVO.getTripItems()){
//				Integer categoryId = itemVO.getCategoryId();
//				TripCategoryVO categoryVO = tripCategoryMap.get(categoryId);
//				if (categoryVO == null){
//					categoryVO = new TripCategoryVO();
//					categoryVO.setCategoryId(categoryId);
//					categoryVO.setCategoryName(itemVO.getCategoryName());
//					categoryVO.setCategoryImg(itemVO.getCategoryImg());
//					tripCategoryMap.put(categoryId, categoryVO);
//				}
//				categoryVO.incrementCount();
//			}
//		}
//		//封装的行程特色类别
//		tripInfoVO.setCategory(tripCategoryMap.values());
		
		tripInfoVO.setResultCode(APPConstants.SUCCESS);
		tripInfoVO.setResultMsg(APPConstants.SUCCESS_MSG);
		return tripInfoVO;
	}
}
